// Learn more about Tauri commands at https://tauri.app/develop/calling-rust/
use tauri::menu::{AboutMetadataBuilder, Menu, PredefinedMenuItem, Submenu};

// MenuBuilder, MenuItem, MenuItemBuilder,

use tauri::tray::TrayIconBuilder;

#[tauri::command]
fn greet(name: &str) -> String {
    format!("Hello, {}! You've been greeted from Rust!", name)
}

#[cfg_attr(mobile, tauri::mobile_entry_point)]
pub fn run() {
    tauri::Builder::default()
        .plugin(tauri_plugin_os::init())
        .plugin(tauri_plugin_global_shortcut::Builder::new().build())
        .plugin(tauri_plugin_clipboard_manager::init())
        .plugin(tauri_plugin_dialog::init())
        .plugin(tauri_plugin_positioner::init())
        .plugin(tauri_plugin_notification::init())
        .plugin(tauri_plugin_http::init())
        .plugin(tauri_plugin_store::Builder::new().build())
        .plugin(tauri_plugin_opener::init())
        .invoke_handler(tauri::generate_handler![greet])
        .menu(|handle| {
            Menu::with_items(
                handle,
                &[
                    &Submenu::with_items(
                        handle,
                        "T2",
                        true,
                        &[
                            &PredefinedMenuItem::about(
                                handle,
                                Some("关于"),
                                Some(
                                    AboutMetadataBuilder::new()
                                        .authors(Some(vec!["李学成".to_string()]))
                                        .name(Some("T2".to_string()))
                                        .version(Some("0.1.0".to_string()))
                                        .build(),
                                ),
                            )
                            .unwrap(),
                            &PredefinedMenuItem::services(handle, Some("服务"))?,
                            &PredefinedMenuItem::separator(handle)?,
                            &PredefinedMenuItem::hide_others(handle, Some("隐藏其他"))?,
                            &PredefinedMenuItem::show_all(handle, Some("显示全部"))?,
                            &PredefinedMenuItem::minimize(handle, Some("最小化"))?,
                            &PredefinedMenuItem::separator(handle)?,
                            &PredefinedMenuItem::fullscreen(handle, Some("全屏"))?,
                            &PredefinedMenuItem::separator(handle)?,
                            &PredefinedMenuItem::close_window(handle, Some("关闭窗口"))?,
                            &PredefinedMenuItem::quit(handle, Some("退出"))?,
                            // #[cfg(target_os = "macos")]
                            // &MenuItem::new(handle, "Hello", true, None::<&str>)?,
                        ],
                    )?,
                    &Submenu::with_items(
                        handle,
                        "编辑",
                        true,
                        &[
                            &PredefinedMenuItem::copy(handle, Some("复制"))?,
                            &PredefinedMenuItem::cut(handle, Some("剪切"))?,
                            &PredefinedMenuItem::paste(handle, Some("粘贴"))?,
                            &PredefinedMenuItem::select_all(handle, Some("全选"))?,
                            &PredefinedMenuItem::undo(handle, Some("撤销"))?,
                            &PredefinedMenuItem::redo(handle, Some("重做"))?,
                            // #[cfg(target_os = "macos")]
                            // &MenuItem::new(handle, "Hello", true, None::<&str>)?,
                        ],
                    )?,
                ],
            )
        })
        // This is required to get tray-relative positions to work
        .setup(|app| {
            TrayIconBuilder::new()
                .on_tray_icon_event(|app, event| {
                    tauri_plugin_positioner::on_tray_event(app.app_handle(), &event);
                })
                .build(app)?;

            // 调试信息
            // println!(menu);

            Ok(())
        })
        .run(tauri::generate_context!())
        .expect("error while running tauri application");
}
